package net.xiaoyu233.mitemod.miteite.trans.entity;

import net.minecraft.*;
import net.xiaoyu233.mitemod.miteite.util.Configs;
import net.xiaoyu233.mitemod.miteite.util.ParticleUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.UUID;

@Mixin(EntityIronGolem.class)
public class EntityIronGolemTrans extends EntityGolem {

    public int DATA_OBJ_IS_BOOSTED;
    @Shadow
    private int attackTimer;



    public EntityIronGolemTrans(World par1World) {
        super(par1World);
    }


    @Overwrite
    protected void applyEntityAttributes() {
        super.applyEntityAttributes();
        int day = this.getWorld() != null ? this.getWorld().getDayOfOverworld() : 0;
        this.getEntityAttribute(GenericAttributes.maxHealth).setAttribute(200.0D + day / 26D);
        this.getEntityAttribute(GenericAttributes.movementSpeed).setAttribute(0.25D);
        this.setEntityAttribute(GenericAttributes.attackDamage, 0.0D);
    }

    public EntityDamageResult attackEntityAsMob(Entity target) {
        this.attackTimer = 10;
        this.worldObj.setEntityState(this, EnumEntityState.golem_throw);
        int day = this.getWorld() != null ? this.getWorld().getDayOfOverworld() : 0;
        EntityDamageResult result = target.attackEntityFrom(new Damage(DamageSource.causeMobDamage(this), (float) (25f + day / 16f + this.getEntityAttributeValue(GenericAttributes.attackDamage))));
        if (result != null) {
            if (result.entityWasKnockedBack()) {
                target.motionY += 0.4000000059604645D;
            }

            this.playSound("mob.irongolem.throw", 1.0F, 1.0F);
        }

        return result;
    }

    protected void entityInit() {
        super.entityInit();
        this.dataWatcher.addObject(16, (byte) 0);
        this.DATA_OBJ_IS_BOOSTED = this.dataWatcher.addObject(this.dataWatcher.getNextAvailableId(), (byte) 0);
    }

    private boolean isBoosted() {
        return this.dataWatcher.getWatchableObjectByte(this.DATA_OBJ_IS_BOOSTED) != 0;
    }

    @Shadow
    private boolean isPlayerCreated() {
        return false;
    }

    @Shadow
    private void setPlayerCreated(boolean playerCreated) {
    }

    @Override
    public boolean onEntityRightClicked(EntityPlayer player, ItemStack item_stack) {
        if (Configs.wenscConfig.canBoostIronGolem.ConfigValue && !this.isBoosted() && item_stack != null && item_stack.isBlock() && item_stack.getItemAsBlock().getBlock() == Block.blockMithril) {
            this.setSize(1.9F, 3.5F);
            this.setEntityAttribute(GenericAttributes.maxHealth, this.getEntityAttributeValue(GenericAttributes.maxHealth) + 20.0D);
            this.setEntityAttribute(GenericAttributes.attackDamage, this.getEntityAttributeValue(GenericAttributes.attackDamage) + 3.0D);
            this.setHealth((float) this.getEntityAttributeValue(GenericAttributes.maxHealth));
            this.dataWatcher.updateObject(this.DATA_OBJ_IS_BOOSTED, (byte) 1);
            if (!this.getWorld().isRemote) {
                player.convertOneOfHeldItem(null);
            }

            return true;
        }  else if (item_stack != null && item_stack.getItem() == Item.ingotIron && this.getHealth() < this.getMaxHealth()) {
            // 恢复生命值（每根铁锭恢复10点生命）
            float healAmount = Math.min(10.0F, this.getMaxHealth() - this.getHealth());
            this.heal(healAmount);

            // 播放修复音效
            this.worldObj.playSoundAtEntity(this, "random.anvil_use", 0.5F, 1.0F);

            // 生成修复粒子效果
            for (int i = 0; i < 5; i++) {

                ParticleUtils.spawnParticle(this.getWorld(),EnumParticle.heart.ordinal(),
                        this.posX + (this.rand.nextDouble() - 0.5) * this.width,
                        this.posY + this.rand.nextDouble() * this.height,
                        this.posZ + (this.rand.nextDouble() - 0.5) * this.width,
                        0, 0, 0);
            }

            // 消耗一个铁锭（服务器端）
            if (!this.getWorld().isRemote) {
                player.convertOneOfHeldItem(new ItemStack(Item.ingotIron, 1));
            }

            return true;
            }
            return super.onEntityRightClicked(player, item_stack);
    }





    @Override
    public void onLivingUpdate() {
        super.onLivingUpdate();
    }

    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) {
        super.readEntityFromNBT(par1NBTTagCompound);
        if (par1NBTTagCompound.hasKey("Boosted")) {
            this.dataWatcher.updateObject(this.DATA_OBJ_IS_BOOSTED, (byte) (par1NBTTagCompound.getBoolean("Boosted") ? 1 : 0));
        }

        this.setPlayerCreated(par1NBTTagCompound.getBoolean("PlayerCreated"));
        if (this.isBoosted()) {
            this.setSize(1.9F, 3.5F);
        }
    }

    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) {
        super.writeEntityToNBT(par1NBTTagCompound);
        par1NBTTagCompound.setBoolean("Boosted", this.isBoosted());
        par1NBTTagCompound.setBoolean("PlayerCreated", this.isPlayerCreated());

    }
}
